﻿using HrEFCore;
using HrEFCore.Models;
using HrIService;
using HrService;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace HrUI.Controllers
{
    public class CFTKController : Controller
    {
        //ICFTKService cFTKService = new CFTKService();
        private readonly MyDbContext myDbContext;
        private readonly CFTKService cFTKService;

        public CFTKController(MyDbContext myDbContext, CFTKService cFTKService)
        {
            this.myDbContext = myDbContext;
            this.cFTKService = cFTKService;
        }
        public async Task<IActionResult> Index()
        {
            List<CFTK> list = await cFTKService.SelectCFTKAsync();
			ViewData.Model = list;
            return View();
        }
        public async Task xllb()//下拉列表
        {
            List<CFFK> list = await cFTKService.CFFKSelectAsync();
			SelectList selectListItems = new SelectList(list, "FirstKindId", "FirstKindName");
			ViewBag.f = selectListItems;

            List<CFSK> list2 = await cFTKService.CFSKSelectAsync();
            SelectList selectListItems1 = new SelectList(list2, "SecondKindId", "SecondKindName");
            ViewBag.s = selectListItems1;
        }
        //二级下拉框
        public async Task<IActionResult> GetSecondKinds(string id)//联动
        {
            List<CFSK> cs = await cFTKService.CFSKLDAsync(id);
            return Json(cs);
        }
		public async Task<IActionResult> GetThirdKinds(string id)//联动
		{
			List<CFTK> ct = await cFTKService.CFTKLDAsync(id);
			return Json(ct);
		}
		[HttpGet]
        public async Task<IActionResult> Add() 
        {
            await xllb();
            return View();
        }
        [HttpPost]
        public async Task<ActionResult> Add(CFTK cftk)
        {
            CFFK cffk = await cFTKService.CFFKByIdAsync(cftk.FirstKindId.ToString());
            cftk.FirstKindName = cffk.FirstKindName;

            CFSK cfsk = await cFTKService.CFSKByIdAsync(cftk.SecondKindId.ToString());
            cftk.SecondKindName = cfsk.SecondKindName;

            int row = await cFTKService.AddCFTKAsync(cftk);
                if (row > 0)
                {
                    return RedirectToAction("Index");
                }
                else
                {
                    return View();
                }
        }
        [HttpGet]
        public async Task<ActionResult> Update(short id) 
        {
            CFTK cftk = await cFTKService.CFTKByIdAsync(id);
            ViewData.Model = cftk;
            await xllb();
            return View();
        }
        [HttpPost]
        public async Task<ActionResult> Update(CFTK cftk)
        {
			CFFK cffk = await cFTKService.CFFKByIdAsync(cftk.FirstKindId.ToString());
            cftk.FirstKindId = cffk.FirstKindId;
			cftk.FirstKindName = cffk.FirstKindName;

			CFSK cfsk = await cFTKService.CFSKByIdAsync(cftk.SecondKindId.ToString());
            cftk.SecondKindId = cfsk.SecondKindId;
			cftk.SecondKindName = cfsk.SecondKindName;
			//if (ModelState.IsValid)
   //         {
                int row = await cFTKService.UpdateCFTKAsync(cftk);
                if (row > 0)
                {
                    return RedirectToAction("Index");
                }
                else
                {
                    await xllb();
                    return View(cftk);
                }
            //}
            //else
            //{
            //    await xllb();
            //    return View(cftk);
            //}
        }
        public async Task<IActionResult> Delete(short id)
        {
            int row=await cFTKService.DeleteCFTKAsync(id);
            if (row > 0)
            {
                return RedirectToAction("Index");
            }
            else
            {
                return RedirectToAction("Index");
            }
        }
    }
}
